<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_MATRIX.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_MATRIX,$login['staff_auth']);

$dao = new Class_M_MATRIX;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';

	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms                = $dao->get( $_GET['id'] );
		$forms["id"]          = $_GET['id'];
		//データ整形開始
		$forms["flier_date"]             = Class_PWD::int8_to_dateD($forms["flier_date"]) ;
		$tempT = "00";
		$forms["draft_create_dt"]        = Class_PWD::dateTime_to_dateD($forms["draft_create_dt"], $tempT);
		$forms["draft_create_t"]         = $tempT;
		
		$forms["draft_input_begin_dt"]   = Class_PWD::dateTime_to_dateD($forms["draft_input_begin_dt"], $tempT);
		$forms["draft_input_begin_t"]    = $tempT;
		
		$forms["draft_input_end_dt"]     = Class_PWD::dateTime_to_dateD($forms["draft_input_end_dt"], $tempT);
		$forms["draft_input_end_t"]      = $tempT;
		
		$forms["draft_modi_begin_dt"]    = Class_PWD::dateTime_to_dateD($forms["draft_modi_begin_dt"], $tempT);
		$forms["draft_modi_begin_t"]     = $tempT;
		
		$forms["draft_modi_end_dt"]      = Class_PWD::dateTime_to_dateD($forms["draft_modi_end_dt"], $tempT);
		$forms["draft_modi_end_t"]       = $tempT;
		
		$forms["draft_publish_begin_dt"] = Class_PWD::dateTime_to_dateD($forms["draft_publish_begin_dt"], $tempT);
		$forms["draft_publish_begin_t"]  = $tempT;
		
		$forms["draft_publish_end_dt"]   = Class_PWD::dateTime_to_dateD($forms["draft_publish_end_dt"], $tempT);
		$forms["draft_publish_end_t"]    = $tempT;
		
		$forms["final_input_begin_dt"]   = Class_PWD::dateTime_to_dateD($forms["final_input_begin_dt"], $tempT);
		$forms["final_input_begin_t"]    = $tempT;
		
		$forms["final_input_end_dt"]     = Class_PWD::dateTime_to_dateD($forms["final_input_end_dt"], $tempT);
		$forms["final_input_end_t"]      = $tempT;
		
		$forms["final_modi_begin_dt"]    = Class_PWD::dateTime_to_dateD($forms["final_modi_begin_dt"], $tempT);
		$forms["final_modi_begin_t"]     = $tempT;
		
		$forms["final_modi_end_dt"]      = Class_PWD::dateTime_to_dateD($forms["final_modi_end_dt"], $tempT);
		$forms["final_modi_end_t"]       = $tempT;
		
		$forms["final_publish_begin_dt"] = Class_PWD::dateTime_to_dateD($forms["final_publish_begin_dt"], $tempT);
		$forms["final_publish_begin_t"]  = $tempT;
		
		//データ整形完了
		
	}else{
		//$forms['auth'] = 0;
		$forms["year4"] = date('Y');
	}
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}

		$arr_week_cd  =  split("_", $forms['week_cd']);
		
		$values["week_cd"]          = $arr_week_cd[0];
		$values["week_name"]        = "第".$values["week_cd"]."週";
		$values["week_seq"]         = $values["week_seq"] ? $values["week_seq"] : 0 ;
		$values["matrix_cd"]        = $forms['year4'].($values["week_cd"] < 10 ? "0".$values["week_cd"] :$values["week_cd"]).$values["week_seq"] ;
		$values["year4"]            = $forms['year4'];
		
		$values["flier_date"]             = Class_PWD::date_to_int8 (substr($forms['flier_date'], 0, 10)) ;
		$values["week_date_begin"]        = Class_PWD::date_to_int8 (substr($forms['week_date_begin'], 0,10)) ;
		$values["week_date_end"]          = Class_PWD::date_to_int8 (substr($forms['week_date_end'], 0, 10)) ;
		
		$values["draft_create_dt"]        = Class_PWD::dateHH_to_time ( $forms['draft_create_dt'], $forms['draft_create_t']) ;
		$values["draft_input_begin_dt"]   = Class_PWD::dateHH_to_time ( $forms['draft_input_begin_dt'], $forms['draft_input_begin_t']) ;
		$values["draft_input_end_dt"]     = Class_PWD::dateHH_to_time ( $forms['draft_input_end_dt'], $forms['draft_input_end_t']) ;
		$values["draft_modi_begin_dt"]    = Class_PWD::dateHH_to_time ( $forms['draft_modi_begin_dt'], $forms['draft_modi_begin_t']) ;
		$values["draft_modi_end_dt"]      = Class_PWD::dateHH_to_time ( $forms['draft_modi_end_dt'], $forms['draft_modi_end_t']) ;
		$values["draft_publish_begin_dt"] = Class_PWD::dateHH_to_time ( $forms['draft_publish_begin_dt'], $forms['draft_publish_begin_t']) ;
		$values["draft_publish_end_dt"]   = Class_PWD::dateHH_to_time ( $forms['draft_publish_end_dt'], $forms['draft_publish_end_t']) ;
		$values["final_input_begin_dt"]   = Class_PWD::dateHH_to_time ( $forms['final_input_begin_dt'], $forms['final_input_begin_t']) ;
		$values["final_input_end_dt"]     = Class_PWD::dateHH_to_time ( $forms['final_input_end_dt'], $forms['final_input_end_t']) ;
		$values["final_modi_begin_dt"]    = Class_PWD::dateHH_to_time ( $forms['final_modi_begin_dt'], $forms['final_modi_begin_t']) ;
		$values["final_modi_end_dt"]      = Class_PWD::dateHH_to_time ( $forms['final_modi_end_dt'], $forms['final_modi_end_t']) ;
		$values["final_publish_begin_dt"] = Class_PWD::dateHH_to_time ( $forms['final_publish_begin_dt'], $forms['final_publish_begin_t']);
		

		if( $err_mes['matrix_cd'] == '' ){
			if( $forms['id'] == '' ){
				$where = sprintf(" matrix_cd = '%s'", $values['matrix_cd'] );
			}else{
				$where = sprintf(" matrix_cd = '%s' AND id <> %s", $forms['matrix_cd'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['year4'] = '<p class="error">年と週組み合わせでは重複しています</p>';
			}
		}

		
		if ( $err->clear ){
			
			//$cols     = $dao->get_cols();
			//foreach($cols as $kcol => $val) {
			//	$values[$kcol] = $forms[$kcol] ;
			//}
			//$values["media_cd"]      =  Class_M_OTHER::do_media($values["media_name"]);

			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
		}
		
		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$forms['id'] = $id;
					$smarty->assign('message', '登録が完了しました。');
					
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['id'];
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);


$smarty->assign('year4',  Class_PWD::get_years() );

$hours = array();

for( $a = 0; $a <= 23; $a++ )
{
	$hours[$a] = "&nbsp;".$a;
}

$smarty->assign('hours', $hours );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mmatrix_form.html');
?>